let $
layui.use(['table', 'admin', 'axJson', 'func', 'form'], function () {
    $ = layui.$;
    let form = layui.form;
    var $ax = layui.axJson;
    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Changein.ctxPath + "/other/configUpdate", function (data) {
            Changein.success("提交成功！");
            window.location.href = Changein.ctxPath + "/other/updateConfig"
        }, function (data) {
            Changein.error("提交失败！" + data.responseJSON.message)
        });
        let changes = []
        $.each($("fieldset").find(".layui-form-item").find("input"), function (i, item) {
            if (item.value) {
                changes.push(item.value)
            }
        })
        data.field.changes = changes
        ajax.setData(JSON.stringify(data.field));
        ajax.start();
        return false;
    });

    var ajax = new $ax(Changein.ctxPath + "/other/getUpdateConfig", function (result) {
        if (result.data) {
            result.data.forceUpdate += ''
        }
        form.val('form', result.data);
        if (result.data.changes) {
            for (let k = 0; k < result.data.changes.length; k++) {
                if (k === 0) {
                    let div = $("fieldset").find(".layui-form-item:first")
                    div.find("input").val(result.data.changes[k])
                } else {
                    creatChanges().find("input").val(result.data.changes[k])
                }
            }
        }
    }, function (data) {
        Changein.error("获取配置失败!" + data.responseJSON.message)
    });
    ajax.start();
})

function creatChanges() {
    let div = $("fieldset").find(".layui-form-item:first").clone();
    div.find("input").val("");
    $("fieldset").find("button").before(div);
    return div
}
